<?php
class ApiController extends Zend_Controller_Action
{
	public function init()
	{
		ini_set('memory_limit', '-1');
	}
	
	public function indexAction()
	{

	}
	
	#get campaign from local system 
	public function postCampaignAction()
	{
		$arrResult = array(
			'status'		=>'fail',
			'reason'		=>'',
			'error_code'	=> '',
			'result'		=>''	
			);
			
		if($this->_request->isPost())
		{
			$country			= $this->_request->getParam('country', null);
			$secrtKey			= $this->_request->getParam('secrtkey', null);
			$strCampaignId		= $this->_request->getParam('campaign_id', null);
			$strSku				= $this->_request->getParam('sku', null);
			$strCampaignName	= $this->_request->getParam('campaign_name', null);
			$strStartDate		= $this->_request->getParam('start_date', null);
			$strEndDate			= $this->_request->getParam('end_date', null);

			if($secrtKey != API_SECRTKEY)
			{
				$arrResult['status'] ='fail';
				$arrResult['reason'] ='secrtkey is wrong';
				echo json_encode($arrResult);
				exit;
			}
			
			if($country =="")
			{
				$arrResult['status'] ='fail';
				$arrResult['reason'] ='country is empty';
				echo json_encode($arrResult);
				exit;
			}

			$arrParams	= array
				(
					'id_campaign_local'	=> $strCampaignId,
					'campaign_name'		=> $strCampaignName,
					'start_date'		=> $strStartDate,
					'end_date'			=> $strEndDate,
					'created_at'		=> new Zend_Db_Expr('NOW()')
					);
			 
			$objCampaign = new QCampaign($country);	
					
			$arrData = $objCampaign->getbyLocalCampaignId($strCampaignId);
			
			$objCampaignSku = new QCampaignSku($country);
			
			if(!$arrData) // record new first time insert
			{
				 $objCampaign->insert($arrParams);
			}
			
			$arrData = $objCampaign->getbyLocalCampaignId($strCampaignId);
			if($arrData)
			{
				$arrParams	= array
					(
						'sku'	=> $strSku,
						'fk_campaign' => $arrData[0]['id']
						);
				$intResult = $objCampaignSku->insert($arrParams);
				if(!$intResult)
				{
					$arrResult['status'] ='fail';
					$arrResult['reason'] ='Insert detail fail';
					echo json_encode($arrResult);	
				}
			}
			else
			{
				$arrResult['status'] ='fail';
				$arrResult['reason'] ='Insert header fail';
				echo json_encode($arrResult);	
			}
			
			if($intResult)
			{
				$arrResult['status'] ='success';
				$arrResult['reason'] ='';
				echo json_encode($arrResult);
			}
			else
			{
				$arrResult['status'] ='fail';
				$arrResult['reason'] ='Insert data is fail at dragon tool';
				echo json_encode($arrResult);
			}
		}
		else
		{
			$arrResult['status'] ='fail';
			$arrResult['reason'] ='This api must use post method';
			echo json_encode($arrResult);
		}
		exit;
	}
	
	public function importCampaignAction()
	{
		$country ='th';
		
		if($this->_request->isPost())
		{
			$objFile	= isset($_FILES['file']) ? $_FILES['file'] : null ;
			
			if(!$objFile['name'])
			{
				echo '<script>parent.show_error("campaigndata", "null")</script>';
				exit;
			}
			$arrKey			= array('campaign_id', 'campaign_name','sku', 'start_date', 'end_date');
			
			$strQuery		= "TRUNCATE TABLE " . $country . "_campaigns_tmp; 
							LOAD DATA LOCAL INFILE '".str_replace('\\', '/', $objFile['tmp_name'])."' 
							INTO TABLE " . $country  . "_campaigns_tmp 
							FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
							LINES TERMINATED BY '\r\n'
							IGNORE 1 LINES (".implode(',', $arrKey).")";
			
			$db		= Zend_Registry::get('db');
			$db->execute($strQuery);
			
			$strQuery ="Select * from " . $country . "_campaigns_tmp;";
			
			$db->execute($strQuery);
			
			$arrData = $db->fetchAll($strQuery);
			
			if($arrData)
			{
				$objCampaignSku = new QCampaignSku($country);	
				$objCampaign = new QCampaign($country);	
				foreach ($arrData as $item)
				{
					
					$objCampaign = new QCampaign($country);	
					$arrDataCampaign = $objCampaign->getbyLocalCampaignId($item['campaign_id']);
					
					if(!$arrDataCampaign)
					{
						$arrParams	= array
							(
								'id_campaign_local'	=> $item['campaign_id'],
								'campaign_name'		=> $item['campaign_name'],
								'start_date'		=> $item['start_date'],
								'end_date'			=> $item['end_date'],
								'created_at'		=> new Zend_Db_Expr('NOW()')
							);
						$objCampaign->insert($arrParams);
					}
		
					$arrDataCampaign = $objCampaign->getbyLocalCampaignId($item['campaign_id']);
					if($arrDataCampaign)
					{
						$arrParams	= array
							(
								'sku'	=> $item['sku'],
								'fk_campaign' => $arrDataCampaign[0]['id']
								);
						$intResult = $objCampaignSku->insert($arrParams);
					}	
				}
			}
			
			$db->closeConnection();
			echo '<script>parent.update_done();</script>' . $objFile;
			exit;
		}
	}
}
?>
